class Order < ActiveRecord::Base
  attr_protected :status
  has_many :order_items,:dependent=>:delete_all
  has_many :products,:through=>:order_items
  belongs_to :user
  validates_size_of :order_items, :minimum =>1
  validates_length_of :ship_to_name, :in => 2..255
  validates_length_of :ship_to, :in => 2..255
  validates_length_of :ship_to_code, :in => 2..255
  validates_length_of :phone_number, :in => 7..20
  validates_format_of :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i

  def total
    order_items.inject(0){|sum,item| item.price*item.amount+sum}
  end
end
